/*! ******************************************************************************
 *
 * Created on 2022年3月5日
 *
 * Copyright(c) YuZhou Big Data Laboratory CO.,Ltd, 2022.  All rights reserved.
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/
package org.yzbdl.lanius.ui.spoon.middle.ext;

import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabFolder2Adapter;
import org.eclipse.swt.custom.CTabFolderEvent;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.pentaho.di.core.Props;
import org.pentaho.di.ui.core.PropsUI;

import org.yzbdl.lanius.ui.spoon.bottom.StateButtonGroup;
import org.yzbdl.lanius.ui.spoon.bottom.StateButtonItemAdapter;
import com.google.common.collect.Maps;

/**
 * @author hs@yzbdl.ac.cn
 *
 */
public class MiddleExt extends Composite implements StateButtonItemAdapter {

	private final SashForm middleSashForm;

	private final CTabFolder tabFolder;

	private final Map<String, CTabItem> tabItems = Maps.newLinkedHashMap();

	private StateButtonGroup stateButtonGroup;

	public MiddleExt(SashForm middleSashForm) {
		this(middleSashForm, SWT.BORDER);
	}

	private MiddleExt(SashForm middleSashForm, int style) {
		super(middleSashForm, style);
		this.middleSashForm = middleSashForm;

		PropsUI.getInstance().setLook(this);
		GridLayout gridLayout = new GridLayout();
		gridLayout.marginHeight = 1;
		gridLayout.marginWidth = 1;
		this.setLayout(gridLayout);

		// tabFolder初始化
		tabFolder = new CTabFolder(this, SWT.NONE | SWT.CLOSE | SWT.BORDER);
		tabFolder.marginHeight = 5;
		tabFolder.marginWidth = 5;
		tabFolder.setMaximizeVisible(true);
		tabFolder.setMinimizeVisible(true);
		tabFolder.addCTabFolder2Listener(new CTabFolder2Adapter() {
			@Override
			public void minimize(CTabFolderEvent event) {
				minimizeTabFolder();
			}

			@Override
			public void maximize(CTabFolderEvent event) {
				maximizeTabFolder();
			}

			@Override
			public void restore(CTabFolderEvent event) {
				restoreTabFolder();
			}
		});
		PropsUI.getInstance().setLook(tabFolder, Props.WIDGET_STYLE_TAB);

		minimizeTabFolder();
	}

	public void minimizeTabFolder() {
		tabFolder.setMinimized(true);
		tabFolder.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false));

		// 设置middleSashForm中第一个子元素(middle canvas)最大化
		middleSashForm.setMaximizedControl(middleSashForm.getChildren()[0]);
		middleSashForm.layout(true);

		// 与状态栏联动
		if (stateButtonGroup != null) {
			stateButtonGroup.setVisible(true);
		}
	}

	public void maximizeTabFolder() {
		tabFolder.setMaximized(true);
		tabFolder.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));

		// 设置middleSashForm中第一个子元素(middle canvas)最大化
		middleSashForm.setMaximizedControl(this);
		middleSashForm.layout(true);
	}
	
	public void restoreTabFolder() {
		tabFolder.setMaximized(false);
		tabFolder.setMinimized(false);
		tabFolder.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));

		middleSashForm.setMaximizedControl(null);
		middleSashForm.layout(true);

		// 与状态栏联动
		if (stateButtonGroup != null) {
			stateButtonGroup.setVisible(false);
		}
	}

	public void addTabItem(MiddleExtTabItem tabItem) {
		tabItems.put(tabItem.getText(), tabItem);
	}

	public void selectTabItem(MiddleExtTabItem item) {
//		if (!tabItems.containsKey(item.getText())) {
//			throw new RuntimeException("TabItem[" + item.getText() + "]不存在");
//		}
		this.tabFolder.setSelection(item);
	}

	public SashForm getMiddleSashForm() {
		return middleSashForm;
	}

	public CTabFolder getTabFolder() {
		return tabFolder;
	}

	public StateButtonGroup getStateButtonGroup() {
		return stateButtonGroup;
	}

	public void setStateButtonGroup(StateButtonGroup stateButtonGroup) {
		this.stateButtonGroup = stateButtonGroup;
	}

	@Override
	public String getStateBarBtnName() {
		return "扩展区";
	}

	@Override
	public String getStateBarBtnImage() {
		return "ui/images/ext.png";
	}

	@Override
	public void onStateBarBtnClick() {
		restoreTabFolder();
	}

}
